package com.wanxs.mall.member.controller;

import com.alibaba.fastjson.JSON;
import com.wanxs.mall.common.annotation.LogOperation;
import com.wanxs.mall.common.constant.Constant;
import com.wanxs.mall.common.exception.BizCodeEnume;
import com.wanxs.mall.common.page.PageData;
import com.wanxs.mall.common.utils.ExcelUtils;
import com.wanxs.mall.common.utils.Result;
import com.wanxs.mall.common.validator.AssertUtils;
import com.wanxs.mall.common.validator.ValidatorUtils;
import com.wanxs.mall.common.validator.group.AddGroup;
import com.wanxs.mall.common.validator.group.DefaultGroup;
import com.wanxs.mall.common.validator.group.UpdateGroup;
import com.wanxs.mall.member.dto.MemberDTO;
import com.wanxs.mall.member.entity.MemberEntity;
import com.wanxs.mall.member.excel.MemberExcel;
import com.wanxs.mall.member.exception.PhoneExsitExecption;
import com.wanxs.mall.member.exception.UsernameExsitException;
import com.wanxs.mall.member.service.MemberService;
import com.wanxs.mall.member.vo.MemberLoginVO;
import com.wanxs.mall.member.vo.MemberReigerVO;
import com.wanxs.mall.member.vo.SocialUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 会员
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-24
 */
@RestController
@RequestMapping("member/member")
@Api(tags="会员")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("member:member:page")
    public Result<PageData<MemberDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<MemberDTO> page = memberService.page(params);

        return new Result<PageData<MemberDTO>>().ok(page);
    }

    @GetMapping("list")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
//    @RequiresPermissions("member:member:page")
    public Result<PageData<MemberDTO>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<MemberDTO> page = memberService.page(params);

        return new Result<PageData<MemberDTO>>().ok(page);
    }

    @GetMapping("info/{id}")
    @ApiOperation("信息")
    @RequiresPermissions("member:member:info")
    public Result<MemberDTO> get(@PathVariable("id") Long id){
        MemberDTO data = memberService.get(id);

        return new Result<MemberDTO>().ok(data);
    }

    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("member:member:save")
    public Result save(@RequestBody MemberDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        memberService.save(dto);

        return new Result();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("member:member:update")
    public Result update(@RequestBody MemberDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        memberService.update(dto);

        return new Result();
    }

    @RequestMapping("delete")
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("member:member:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        memberService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("member:member:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<MemberDTO> list = memberService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, MemberExcel.class);
    }

    /**
     * 会员注册
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody MemberReigerVO vo){
        try {
            memberService.register(vo);
        }catch (UsernameExsitException exception){
            return new Result().error(BizCodeEnume.USERNAME_EXSIT_EXCEPTION.getCode(),
                    BizCodeEnume.USERNAME_EXSIT_EXCEPTION.getMsg());
        }catch (PhoneExsitExecption exsitExecption) {
            return new Result().error(BizCodeEnume.PHONE_EXSIT_EXCEPTION.getCode(),
                    BizCodeEnume.PHONE_EXSIT_EXCEPTION.getMsg());
        }catch (Exception e){
            return new Result().error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(),
                    BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
        }

        return new Result();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody MemberLoginVO vo){
        MemberEntity entity = memberService.login(vo);
        if(entity != null){
            return new Result().ok(JSON.toJSONString(entity));
        }

        return new Result().error(BizCodeEnume.USERNAME_PHONE_VALID_EXCEPTION.getCode(),
                BizCodeEnume.USERNAME_PHONE_VALID_EXCEPTION.getMsg());
    }


    @RequestMapping("/oauth2/login")
    public Result socialLogin(@RequestBody SocialUser vo){
        MemberEntity entity = memberService.login(vo);

        return new Result( ).ok(JSON.toJSONString(entity));
    }

}